﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Animation;
using System.Windows;

using TouchControls.Pivot;
using System.Windows.Controls;

namespace TouchControls
{
    public partial class TouchPivot
    {
        private void ScrollNext()
        {
            var newIndex = SelectedIndex + 1;
            if (newIndex >= Items.Count) newIndex = 0;

            Storyboard stb = new Storyboard();
            Resources.Add("ScrollNextTranslate" + stb.GetHashCode(), stb);

            var item = (PivotItem)Items[SelectedIndex];

            stb.DoubleAnim("ITEMSTRANSLATE", "X", toValue: -ActualWidth);
            stb.DoubleAnim("HEADERSTRANSLATE", "X", toValue: - item.Header.ActualWidth);
            
            stb.Completed += (sender, args) =>
            {
                item.Visibility = Visibility.Collapsed;
                Resources.Remove("ScrollNextTranslate" + stb.GetHashCode());
                stb.Stop();
                stb.Children.Clear();
                SetSelectedIndex(newIndex);
                var prevItem = this.GetPrevItem();
                prevItem.Header.StateInactive();
                item = ((PivotItem)Items[SelectedIndex]);
                item.Visibility = Visibility.Visible;
                item.ShowFromRight();
            };

            stb.Begin();
        }

        private void ScrollPrev()
        {
            var newIndex = SelectedIndex - 1;
            if (newIndex < 0)
                newIndex = Items.Count - 1;
            
            Storyboard stb = new Storyboard();
            Resources.Add("ScrollNextTranslate" + stb.GetHashCode(), stb);

            var item = (PivotItem)Items[SelectedIndex];            

            stb.DoubleAnim("ITEMSTRANSLATE", "X", toValue: ActualWidth);
            stb.DoubleAnim("HEADERSTRANSLATE", "X", toValue: _ghostHeader.ActualWidth);

            stb.Completed += (sender, args) =>
                {
                    item.Visibility = Visibility.Collapsed; 
                    Resources.Remove("ScrollPrevTranslate" + stb.GetHashCode());
                    stb.Stop();
                    stb.Children.Clear();
                    SetSelectedIndex(newIndex);
                    item = ((PivotItem)Items[SelectedIndex]);
                    item.Visibility = Visibility.Visible;
                    item.ShowFromLeft();
                };
            stb.Begin();
        }

        private void ScrollTo(int index)
        {
            if (index >= Items.Count) index = 0;
            if (index < 0)
                index = Items.Count - 1;

            Storyboard stb = new Storyboard();
            Resources.Add("ScrollNextTranslate" + stb.GetHashCode(), stb);
            double offset = _firstMenuMargin;
            var cursor = SelectedIndex;
            
            while (cursor != index)
            {
                var currentitem = (PivotItem)Items[cursor];
                offset += currentitem.Header.ActualWidth;
                cursor++;
                if (cursor >= Items.Count) cursor = 0;
            }

            stb.DoubleAnim("ITEMSTRANSLATE", "X", toValue: ActualWidth, duration : 500);
            stb.DoubleAnim("HEADERSTRANSLATE", "X", toValue: -offset, duration: 500);
            var item = ((PivotItem)Items[SelectedIndex]);
            
            stb.Completed += (sender, args) =>
            {
                Resources.Remove("ScrollPrevTranslate" + stb.GetHashCode());
                item.Visibility = Visibility.Collapsed;
                stb.Stop();
                stb.Children.Clear();
                SetSelectedIndex(index);
                item = ((PivotItem)Items[SelectedIndex]);
                item.Visibility = Visibility.Visible;
                item.ShowFromRight();
            };
            stb.Begin();
        }

        private void SetSelectedIndex(int newIndex)
        {
            this.SetValue(SelectedIndexProperty, newIndex);
            this._itemsPositionDirty = true;
            this.PositionItems();
            var selectedItem = (PivotItem)Items[SelectedIndex];
            if (SelectedItemChanged != null)
                SelectedItemChanged(this, new PivotItemEventArgs(selectedItem));

            selectedItem.ActivateItem();
        }

        private void CancelTranslationAnimation()
        {
            if (Math.Abs(_itemsTranslate.X) < 40)
            {
                this._itemsTranslate.X = 0;
                this._headersTranslate.X = 0;
            }
            else
            {
                Storyboard stb = new Storyboard();
                Resources.Add("CancelTranslate" + stb.GetHashCode(), stb);
                var ease = new QuadraticEase();
                //ease.Springiness = 0.0005;
                //ease.Oscillations = 2;
                ease.EasingMode = EasingMode.EaseOut; 
                
                stb.DoubleAnim("ITEMSTRANSLATE", "X", toValue: 0, easing:ease);
                stb.DoubleAnim("HEADERSTRANSLATE", "X", toValue: 0, easing: ease);
                
                stb.Completed += (sender, args) =>
                {
                    Resources.Remove("CancelTranslate" + stb.GetHashCode());
                    stb.Stop();
                    stb.Children.Clear();
                    this._itemsTranslate.X = 0;
                    this._headersTranslate.X = 0;
                };

                stb.Begin();
            }
        }

        
    }
}
